<template>
  <div>
    <h2>用户借款数据表</h2>
    <table class="table table-bordered">
      <tr>
        <td>借款名称</td>
        <td>
          <input type="text" v-model="state.B_name" class="form-control">
        </td>
      </tr>
      <tr>
        <td>借款最高金额</td>
        <td>
          <input type="text" v-model="state.B_maxPrice" class="form-control">
        </td>
      </tr>
      <tr>
        <td>借款年限</td>
        <td>
          <input type="text" v-model="state.B_year" class="form-control">
        </td>
      </tr>
      <tr>
        <td>利息</td>
        <td>
          <select  v-model="state.I_id" class="form-control">
            <option value="0">--请选择--</option>
            <option v-for="item in list" :value="item.I_id">{{ item.I_name }}</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>借款简介</td>
        <td>
          <input type="text" v-model="state.B_remark" class="form-control">
          <!-- 最高借款1000000元,走过不要错过 -->
        </td>
      </tr>
      <tr>
        <td>用户角色</td>
        <td>
          <select  v-model="state.U_id" class="form-control">
            <option value="0">--请选择--</option>
            <option v-for="item in valt" :value="item.U_id">{{ item.U_name }}</option>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="button" @click="add" value="添加" class="btn btn-primary">
        </td>
      </tr>
    </table>
  </div>
</template>
<script setup lang="ts">
import {reactive,toRefs,ref,onMounted} from 'vue'
import { useRouter } from 'vue-router'
const router=useRouter()
import axios from 'axios'

const data=reactive({
  state:{
    B_name:'',
    B_maxPrice:'',
    B_year:'',
    I_id:'0',
    B_remark:'',
    U_id:'0',
  }
})

const {state}=toRefs(data)


const list =ref([{
  I_id:0,
  I_name:''
}])


const valt=ref([{
  U_id:0,
  U_name:''
}])

onMounted(()=>{
  BackFill1()
  BackFill2()
})

const BackFill1=()=>{
  axios({
    url:'http://localhost:54566/api/Borrow/GetInverestBind',
    mathod:'get',
  })
  .then((res)=>{
    list.value=res.data
  })
  .catch((err)=>{
    console.log(err)
  })
}


const BackFill2=()=>{
  axios({
    url:'http://localhost:54566/api/Borrow/GetUserType',
    mathod:'get',
  })
  .then((res)=>{
    valt.value=res.data
  })
  .catch((err)=>{
    console.log(err)
  })
}


const add=()=>{
  axios({
    url:'http://localhost:54566/api/Borrow/PostBorrowInvest',
    method:'post',
    data:state.value
  })
  .then(res=>{
    console.log(res.data)
    if(res.data>0){
      alert("添加成功")
      return
    }else{

      alert("添加失败")
      return
    }
  })
  .catch(err=>{
    console.log(err)
  })
}
</script>